//
//  NSObject+MDHHook.m
//  MDHPT
//
//  Created by 高得华 on 2019/9/20.
//  Copyright © 2019 高得华. All rights reserved.
//

#import "NSObject+MDHHook.h"
#import <objc/runtime.h>
#import "MDHFileConstant.h"

@implementation NSObject (MDHHook)
@end


@implementation UIViewController (MDHHook)

+ (void)load {
    Method pvacOld = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
    Method pvacNew = class_getInstanceMethod(self, @selector(mdh_presentViewController:animated:completion:));
    method_exchangeImplementations(pvacOld, pvacNew);
}

///进行 适配iOS 13.0新版本的 present的问题
- (void)mdh_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if (@available(iOS 13.0, *)) {
        if (viewControllerToPresent.modalPresentationStyle == UIModalPresentationAutomatic) {
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverFullScreen;
        }
    }
    [self mdh_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end


@implementation NSURL (MDHHook)

+ (void)load {
    Method URLWithStringOld = class_getClassMethod(self, @selector(URLWithString:));
    Method URLWithStringNew = class_getClassMethod(self, @selector(mdh_URLWithString:));
    ///进行交换
    method_exchangeImplementations(URLWithStringOld, URLWithStringNew);
}

///检测URL中是否含有中文 则进行回避
+ (instancetype)mdh_URLWithString:(NSString *)URLString {
    if (URLString.length <= 0) {
        URLString = @"";
    }
    NSURL * url = [NSURL mdh_URLWithString:URLString];
    if (url == nil) {
    #ifdef DEBUG//测试环境
        NSString * mes = [NSString stringWithFormat:@"出现中文了:%@",URLString];
        DTLog(@"%@",mes);
        AlertViewShowOne(mes);
    #else
    #endif
        url = [NSURL mdh_URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    return url;
}

@end
